<?php
/**
 * +----------------------------------------------------------------------
 * | 九月科技-ztuc.cn
 * +----------------------------------------------------------------------
 *                      .::::.
 *                    .::::::::.            | AUTHOR: siyu
 *                    :::::::::::           | EMAIL: ztucke@ztuc.cn
 *                 ..:::::::::::'           | DATETIME: 2020/01/31
 *             '::::::::::::'
 *                .::::::::::
 *           '::::::::::::::..
 *                ..::::::::::::.
 *              ``::::::::::::::::
 *               ::::``:::::::::'        .:::.
 *              ::::'   ':::::'       .::::::::.
 *            .::::'      ::::     .:::::::'::::.
 *           .:::'       :::::  .:::::::::' ':::::.
 *          .::'        :::::.:::::::::'      ':::::.
 *         .::'         ::::::::::::::'         ``::::.
 *     ...:::           ::::::::::::'              ``::.
 *   ```` ':.          ':::::::::'                  ::::..
 *                      '.:::::'                    ':'````..
 * +----------------------------------------------------------------------
 */
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/4/10
 * Time: 21:58
 */
namespace app\store\controller;
use app\core\InitController;
use think\facade\View;
use think\facade\Request;
use app\store\controller\Upload;
use app\store\model\Notice;
use app\store\model\Navbar;
use app\store\model\Article as ArticleModel;
use app\store\model\Image as ImageModel;
use app\store\model\Keyword as Words;
/**
 * 文章
 * Class Article
 * @package app\controller\admin
 */
class Article extends InitController
{
    
    //// 通用搜索
   public function search_json()
    {
        $keywords = $this->request->get('search');
        
        if(!empty($keywords)){
            $map=[['title','like','%'.$keywords.'%'],['status', '=', 1]]; 
        } 
        else{
            $map=true;
        }
        $data = ArticleModel::where($map)->order('id desc')       
        ->paginate(['list_rows' => 8, 'query' => request()->param()])
            ->each(function ($item, $key) {
                $item['image'] = ImageModel::image($item['id'], Upload::TYPE_ARTICLE);
                $data = ArticleModel::flag($item['flag']);
                $item['flag'] = $data == 1 ? '最5新' : ($data == 2 ? '热门' : ($data == 3 ? '推荐' : '未知'));
                $count = Notice::where('article_id', $item['id'])->count();
                $item['count'] = isset($count)?$count:0;
                $item['cat_name'] = Navbar::findNavbar($item['cat_id'])['cat_name'];
                return $item;
            });
            add_user_log('search_json', '关键字搜索');// 记录行为日志
        return view('search', ['data' => $data, 'keywords' => $keywords]);
    }
  
    /**
     * 新增
     * @return array
     * @author: MK
     * @Time: 2020/4/10 22:12
     */
    public function add()
    {
        if ($this->request->isPost()) {
        $param = Request::param();
         /*过滤关键字*/
        $model = Words::keyWordCheck();
         $item = [];

         foreach($model as $k=>$v)
         {
            $item[$k] = $v['word'];
         }
        $param['content'] = str_replace($item,"<font color='#ff0000;'>{此处关键词违规}</font>",$param['content']);
      
        $data = ArticleModel::add($param);

        
        if ($data == true) {
        add_user_log('add', '文章');
                return $this->success('', '', '新增成功');
            }
    }
      
        $data = Navbar::navbar();
        $tree = $this->dropTree($data,0,0);
      
        return $this->fetch('article', ['name' => $tree]);
    }
  
}